using System;
using System.Threading.Tasks;
using GomokuGame.AI;

namespace GomokuGame
{
    /// <summary>
    /// 演示程序
    /// </summary>
    public class Demo
    {
        public static async Task RunDemo()
        {
            Console.WriteLine("=== 五子棋人机对战演示 ===\n");

            // 演示1：人机对战
            Console.WriteLine("演示1：人机对战（AI执白，中级难度）");
            await DemoHumanVsAI();

            Console.WriteLine("\n" + new string('-', 50) + "\n");

            // 演示2：AI对战
            Console.WriteLine("演示2：AI对战（中级难度）");
            await DemoAIVsAI();

            Console.WriteLine("\n演示完成！");
        }

        private static async Task DemoHumanVsAI()
        {
            var gameEngine = new GameEngine();
            
            // 开始人机对战
            gameEngine.StartNewGame(GameMode.HumanVsAI, AIDifficulty.Intermediate, PieceType.White);
            
            Console.WriteLine($"游戏模式：{gameEngine.GameMode}");
            Console.WriteLine($"AI难度：{AIFactory.GetDifficultyName(gameEngine.GetAIDifficulty())}");
            Console.WriteLine($"AI执子：{(gameEngine.GetAIPlayer() == PieceType.Black ? "黑" : "白")}");
            Console.WriteLine($"当前玩家：{(gameEngine.CurrentPlayer == PieceType.Black ? "黑" : "白")}");
            
            // 模拟玩家落子
            Console.WriteLine("\n玩家落子：(7, 7)");
            gameEngine.PlacePiece(7, 7);
            
            // 等待AI思考
            Console.WriteLine("等待AI思考...");
            var startTime = DateTime.Now;
            while (gameEngine.IsAITurn() && (DateTime.Now - startTime).TotalSeconds < 5)
            {
                await Task.Delay(100);
                var aiManager = gameEngine.GetAIManager();
                if (aiManager?.CurrentAI != null)
                {
                    int progress = aiManager.CurrentAI.GetThinkingProgress();
                    Console.Write($"\rAI思考进度：{progress}%");
                }
            }
            
            Console.WriteLine();
            Console.WriteLine($"AI移动完成，总步数：{gameEngine.MoveHistory.Count}");
        }

        private static async Task DemoAIVsAI()
        {
            var gameEngine = new GameEngine();
            
            // 开始AI对战
            gameEngine.StartNewGame(GameMode.AIVsAI, AIDifficulty.Intermediate, PieceType.White);
            
            Console.WriteLine($"游戏模式：{gameEngine.GameMode}");
            Console.WriteLine($"AI难度：{AIFactory.GetDifficultyName(gameEngine.GetAIDifficulty())}");
            
            // 观察AI对战
            Console.WriteLine("\n开始AI对战...");
            var startTime = DateTime.Now;
            int lastMoveCount = 0;
            
            while (gameEngine.CurrentState == GameState.Playing && 
                   (DateTime.Now - startTime).TotalSeconds < 15)
            {
                await Task.Delay(500);
                
                int currentMoveCount = gameEngine.MoveHistory.Count;
                if (currentMoveCount > lastMoveCount)
                {
                    var lastMove = gameEngine.MoveHistory[currentMoveCount - 1];
                    string player = lastMove.PieceType == PieceType.Black ? "黑" : "白";
                    Console.WriteLine($"第{currentMoveCount}步：{player}子落子 ({lastMove.Row}, {lastMove.Col})");
                    lastMoveCount = currentMoveCount;
                }
            }
            
            Console.WriteLine($"AI对战结束，总步数：{gameEngine.MoveHistory.Count}");
            Console.WriteLine($"游戏状态：{gameEngine.CurrentState}");
        }
    }
} 